package com.t2t.top.devops.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类.
 * <p/>
 * 计量系统中统一采用长整型日期Long.
 * time08: 例，20170901
 * time12: 例，201709011711
 * time14: 例，20170901171120
 *
 * @author yangpengfei
 */
public class DateUtils {

    public static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_TIME14 = "yyyyMMddHHmmss";
    public static final String FORMAT_TIME12 = "yyyyMMddHHmm";
    public static final String FORMAT_TIME08 = "yyyyMMdd";
    public static final String FORMAT_TIME06 = "yyyyMM";

    public static Date parse(String timeStr) {
        return parse(timeStr, FORMAT_TIME);
    }

    public static Date parse14(Object timeStr) {
        return parse(timeStr + "", FORMAT_TIME14);
    }

    public static Date parse12(Object timeStr) {
        return parse(timeStr + "", FORMAT_TIME12);
    }

    public static Date parse08(Object timeStr) {
        return parse(timeStr + "", FORMAT_TIME08);
    }

    public static Date parse06(Object timeStr) {
        return parse(timeStr + "", FORMAT_TIME06);
    }

    public static String format(Date date) {
        return format(date, FORMAT_TIME);
    }

    public static String format14(Date date) {
        return format(date, FORMAT_TIME14);
    }

    public static String format12(Date date) {
        return format(date, FORMAT_TIME12);
    }

    public static String format08(Date date) {
        return format(date, FORMAT_TIME08);
    }

    public static String format06(Date date) {
        return format(date, FORMAT_TIME06);
    }

    public static Date parse(String timeStr, String formatPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
        try {
            return sdf.parse(timeStr);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static String format(Date date, String formatPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }



}
